<?php
class star{
	function add($id,$star){
		global $dbs,$user ,$dataType;
		
		$star = (int)$star; 
		if( $star > TASK_STAR && $star > MEDIA_STAR ){
			if( 'ajax' == $dataType )
				exit('{"status":"error","msg":"参数非法，此分数不合法."}');
			else
				exit(infoPage('参数非法，此分数不合法.','',GOTO_TIME));
		}
			
		
		
		
		
		$sql = 'SELECT * FROM `'.DB_PRE.'star` WHERE `user_id` = '.$user -> id() .' and `file_id` ='. $id ;
		$lists = $dbs -> select($sql);
		
		$array['file_id']		= $id;
		$array['user_id']		= $user -> id();
		$array['star']			= (int)$star;		
		$array['ip']			= ip();
		
		
		
		if(count($lists) != 0 ){
			/*
			if( 'ajax' == $dataType)
				exit('{"status":"error","msg":"您已经评价过了.只能评价一次. ^_^"}');
			else
				exit(infoPage('您已经评价过了.只能评价一次. ^_^','',GOTO_TIME));
			*/
			$array['modified']		= date('Y-m-d H:i:s');
			$dbs -> update(DB_PRE.'star' , $array , ' `user_id` = '.$user -> id() .' and `file_id` ='. $id);
			
		}else{			
			$array['created']		= date('Y-m-d H:i:s');			
			$dbs -> insert(DB_PRE.'star',$array);
		}
		
		$sql = 'SELECT AVG(`star`) as `avgs` FROM `'.DB_PRE.'star` WHERE `file_id` ='. $id ;
		$avg = $dbs -> select($sql);
		$avg = $avg[0]['avgs'];
		
		$avgs['star'] = round( $avg );
		$dbs -> update(DB_PRE.'file' ,$avgs , '`id` = '. $id);		
		
		if( 'ajax' == $dataType)
			exit('{"status":"ok","msg":"评分已保存"}');
		else
			exit(infoPage('评分已保存','',GOTO_TIME));
		
	}
	
	function addAssess($file_id,$task_id,$invite,$user_id=0,$timeEnd,$star){
		global $dbs,$user ,$dataType;
		
		// 再检测一次，不信任加密连接
		if( 0 == $user_id)
			$sql = 'SELECT * FROM `'.DB_PRE.'star_assess` WHERE `task_id` = '.$task_id .' and `file_id` = '.$file_id .' and `invite` = "'.$invite.'" and `timeEnd` = "'. $timeEnd .'" ';
		else
			$sql = 'SELECT * FROM `'.DB_PRE.'star_assess` WHERE `task_id` = '.$task_id .' and `file_id` = '.$file_id .' and `user_id` = '.$user_id ;
		$lists = $dbs -> select($sql);
		$array['task_id']		= $task_id;
		$array['file_id']		= $file_id;
		$array['invite']		= $invite;
		$array['user_id']		= $user_id;
		$array['timeEnd']		= $timeEnd;
		$array['star']			= $star;		
		$array['ip']			= ip();
		
		
		
		
		/*
			if( 'ajax' == $dataType)
				exit('{"status":"error","msg":"您已经评阅. ^_^"}');
			else
				exit(infoPage('您已经评价过了.只能评价一次. ^_^','',GOTO_TIME));
			*/
		$score = $dbs -> select('SELECT * FROM `'.DB_PRE.'task_attend` WHERE `task_id` = '.$task_id.' and `file_id` ='. $file_id );
		$score = $score[0]['score'];
		
		
		if(count($lists) != 0 ){
			
			// 修改分数，
			$arrayScore['score'] = $score - $lists[0]['star'] + $star;
			
			$array['modified']		= date('Y-m-d H:i:s');
			$dbs -> update(DB_PRE.'star_assess' , $array , ' `task_id` = '.$task_id.' and `file_id` ='. $file_id.' and `invite` = "'.$invite.'" and `timeEnd` = "'. $timeEnd .'" ');
			
			
		}else{			
			$array['created']		= date('Y-m-d H:i:s');			
			$dbs -> insert(DB_PRE.'star_assess',$array);
			
			// 直接累加
			$arrayScore['score'] = $score + $star;
		}
		
		$dbs -> update(DB_PRE.'task_attend' , $arrayScore , ' `task_id` = '.$task_id.' and `file_id` ='. $file_id );
		
		
		if( 'ajax' == $dataType)
			exit('{"status":"ok","msg":"评分已保存"}');
		else
			exit(infoPage('评分已保存','',GOTO_TIME));
		
	}
	
	function creatScore($num){
		$html = '';
		for($i = 1; $i <= 5 ; $i++){
			$class = $i <= $num ? ' class="a" ' : '';
			$html .= '<i'.$class.'></i>';
		}
		return $html;
	}
	
	
	
	function getStar($file_id){
		global $dbs,$user ,$dataType;
		$sql = 'SELECT avg(`star`) as stars FROM `'.DB_PRE.'star` WHERE `file_id` ='. $file_id ;
		$lists = $dbs -> select($sql);
		$lists = $lists[0];
		return round($lists['stars']);
	}
	
	function isStar($file_id,$user_id){
		global $dbs,$user ,$dataType;
		$sql = 'SELECT * FROM `'.DB_PRE.'star` WHERE `user_id` = '.$user_id .' and `file_id` ='. $file_id ;
		$lists = $dbs -> select($sql);
		if(count($lists) != 0 ){
			return true;
		}else{
			return false;
		}
	}
	
}
?>